package com.damai.redis;

import com.alibaba.fastjson.util.ParameterizedTypeImpl;
import com.damai.util.StringUtil;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

public class CacheUtil {

    public static final TimeUnit DEFAULT_TIME_UNIT = TimeUnit.SECONDS;

    //在运行时动态构建复杂嵌套的泛型类型,例如Map<List<String>,Integer>
    //ParameterizedType:表示参数化类型,即带有泛型参数的类型,例如List<String>,是Type类的子接口;
    //其中List是原始类型,String是泛型参数;
    /**一个ParameterizedTypeImpl实例通常会提供以下信息:
     * getActualTypeArguments():返回泛型参数类型的数组(如[String.class]);
     * getOwnerType():返回包含此参数化类型的类型(如果适用,例如对于Outer.Inner<String>,可能是Outer.Inner.class或Outer.class,具体取决于实现)
     * getRawType():返回参数化类型的原始类型(如List.class);
     **/
    public static Type buildType(Type... types) {
        ParameterizedTypeImpl beforeType = null;
        if (types != null && types.length > 0) {
            if(types.length == 1) {
                return new ParameterizedTypeImpl(new Type[]{null}, null, types[0]);
            }
        }
        for(int i = types.length-1; i > 0; i--) {
            beforeType = new ParameterizedTypeImpl(new Type[]{beforeType == null ? types[i] : beforeType}, null, types[i-1]);
        }
        return beforeType;
    }

    /**
     * 检查Key是否为空或空的字符串
     */
    public static void checkNotBlank(String... key) {
        for (String s : key) {
            if (StringUtil.isEmpty(s)) {
                throw new RuntimeException("请求参数缺失");
            }
        }
    }

    /**
     * 检查redisKeyBuild中的key是否为空或空的字符串
     */
    public static void checkNotBlank(RedisKeyBuild redisKeyBuild) {
        if (StringUtil.isEmpty(redisKeyBuild.getRelKey())) {
            throw new RuntimeException("请求参数缺失");
        }
    }

    /**
     * 检查list是否为空或空的字符串
     */
    public static void checkNotBlank(Collection<String> list) {
        for (String s : list) {
            if (StringUtil.isEmpty(s)) {
                throw new RuntimeException("请求参数缺失");
            }
        }
    }

    /**
     * 检查list是否为空或空的字符串
     */
    public static void checkNotEmpty(Collection<?> list) {
        for (Object o : list) {
            if (o == null) {
                throw new RuntimeException("请求参数缺失");
            }
        }
    }

    /**
     * 检查object是否为空
     */
    public static void checkNotEmpty(Object object) {
        if (isEmpty(object)) {
            throw new RuntimeException("请求参数缺失");
        }
    }

    /**
     * 判断object是否为空
     */
    public static boolean isEmpty(Object object) {
        if (object == null) {
            return true;
        }
        if (object instanceof String) {
            return StringUtil.isEmpty((String) object);
        }
        if (object instanceof Collection) {
            return ((Collection<?>) object).isEmpty();
        }
        return false;
    }

    public static List<String> getBatchKey(Collection<RedisKeyBuild> list){
        return list.stream().map(RedisKeyBuild::getRelKey).collect(Collectors.toList());
    }

    public static <T> List<T> optimizeRedisList(List<T> list){
        if (Objects.isNull(list)) {
            return new ArrayList<>();
        }
        if (list.size() == 0 || Objects.isNull(list.get(0))) {
            return new ArrayList<>();
        }
        return list;
    }

    public static boolean checkRedisListIsEmpty(List<?> list){
        if (Objects.isNull(list)) {
            return true;
        }
        return list.size() == 0 || Objects.isNull(list.get(0));
    }
}
